import { useData } from "@sker/runtime"
import { toSuccess, useEntityManager } from "@sker/core"
import { z } from "zod"
import { KsTopic } from "@sker/entities"
import { In } from "typeorm"


export default async () => {
    const input = z.object({
        topics: z.array(z.string())
    })
    const body = input.parse(useData())
    await useEntityManager(async m => {
        const list = await m.find(KsTopic, { where: { title: In(body.topics) } })
        const entities = body.topics.map(it => {
            const item = list.find(item => item.title === it)
            return m.create(KsTopic, {
                id: item?.id,
                title: it
            })
        })
        await m.save(KsTopic, entities)
    })
    return toSuccess(true)
}
